<?php

/**
 * this class provides functions for working with tree nodes
 * @author		ungerboeck simon <su@militaerversand.at>
 *
 */
class TreeNode {
	private $text = null;
	private $tree = null;
	private $id = null;
	private $state = null;
	private $checked = null;

	/************************************************************************
	 ************************** PUBLIC FUNCTIONS ****************************
	 ************************************************************************/

	/**
	 * sets the text
	 * @param 		string		 $text
	 */
	public function __construct($text, $id = null) {
		$this->text = $text;
		$this->id = $id;
		$this->state = 'open';
		$this->checked = false;
	}


	/**
	 * adds a sub node to current node
	 * if the node doesnt have sub nodes, create a new tree first
	 * @param 		mixed		 $node
	 */
	public function addNode(TreeNode $node) {
		if ($this->tree == null) {
			$this->tree = new Tree();
			$this->tree->setIsSubTree(true);
		}

		$this->tree->addNode($node);
	}


	/**
	 * creates the tree node
	 * @return		string		$output
	 */
	public function create() {
		$output = '<li state="' . $this->state . '" ';
		$output .= (($this->checked) ? 'checked ' : '');
		$output .= (isset($this->id) ? 'id="' . $this->id. '"' : '');
		$output .= '>';
		$output .= '<span>' . $this->text . '</span>';
		$output .= ($this->tree != null) ? $this->tree->create() : '';
		$output .= '</li>';
		return $output;
	}
	
	
	/**
	 * closes the node
	 */
	public function close(){
		$this->state = 'closed';
	}
	
	
	/**
	 * opens the node
	 */
	public function open(){
		$this->state = 'open';
	}
	
	
	/**
	 * checkes the node
	 */
	public function check(){
		$this->checked = true;
	}


	/************************************************************************
	 **************************** GETTER / SETTER ***************************
	 ************************************************************************/


	public function setText($text) {
		$this->text = $text;
	}


	public function setId($id) {
		$this->id = $id;
	}


	public function getText($text) {
		return $this->text;
	}
	
	
	public function getId(){
		return $this->id;
	}


	/************************************************************************
	 ************************** PRIVATE FUNCTIONS ***************************
	 ************************************************************************/
}
?>